/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)ResultSetClient2.java	1.13 02/01/12
 */

package jdbcts.resultSet;

import java.io.*;
import java.util.*;
import java.math.*;
import java.sql.*;

import javax.sql.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sun.cts.util.*;
import com.sun.cts.porting.*;
import com.sun.cts.harness.*;

import java.rmi.RemoteException;

import javasoft.sqe.javatest.Status;
import jdbcts.SuperTest;
import jdbcts.common.DataSourceConnection;
import jdbcts.common.DriverManagerConnection;
import jdbcts.common.dbSchema;
import jdbcts.common.rsSchema;

import com.sun.cts.tests.jdbc.ee.common.*;

// Merant DataSource class
//import com.merant.sequelink.jdbcx.datasource.*;

/**
 * The ResultSetClient2 class tests methods of resultSet interface using Sun's
 * J2EE Reference Implementation.
 * 
 * @author
 * @version 1.7, 99/10/12
 */

public class ResultSetClient2 extends SuperTest implements Serializable
{
    private static final String testName = "jdbc.ee.resultSet.resultSet11";

    // Naming specific member variables
    private CTSNamingContextInterface jc = null;

    // Harness requirements
    private transient static Connection conn = null;

    private transient static DatabaseMetaData dbmd = null;

    private static Statement stmt = null;

    private PreparedStatement pstmt = null;

    private DataSource ds1 = null;

    private static String drManager = null;

    private String sqlStmt = null;

    private static dbSchema dbSch = null;

    private static rsSchema rsSch = null;

    private Properties props = null;

    private Properties sqlp = null;

    /* Run test in standalone mode */
    // public static void main(String[] args)
    // {
    // ResultSetClient2 theTests = new ResultSetClient2();
    // Status s = theTests.run(args, System.out, System.err);
    // s.exit();
    // }
    /* Test setup: */

    /*
     * @class.setup_props: Driver, the Driver name; db1, the database name with
     * url; user1, the database user name; password1, the database password;
     * db2, the database name with url; user2, the database user name;
     * password2, the database password; DriverManager, flag for DriverManager;
     * ptable, the primary table; ftable, the foreign table; cofSize, the
     * initial size of the ptable; cofTypeSize, the initial size of the ftable;
     * binarySize, size of binary data type; varbinarySize, size of varbinary
     * data type; longvarbinarySize, size of longvarbinary data type;
     * 
     * @class.testArgs: -ap ctssql.stmt
     */
    @BeforeClass
    // public void setup(String[] args, Properties p) throws Exception
    public static void setup() throws Exception
    {
	try
	{
	    try
	    {
		// props=p;
		// drManager = p.getProperty("DriverManager","");
		drManager = isDRManager;

		if (drManager.length() == 0)
		    throw new Exception("Invalid DriverManager Name");
		/*
		 * sqlp=new Properties(); sqlStmt= p.getProperty("rsQuery","");
		 * InputStream istr= new FileInputStream(sqlStmt);
		 * sqlp.load(istr);
		 */
		// sqlp = p;
		if (drManager.equals("yes"))
		{
		    logMsg("Using DriverManager");
		    DriverManagerConnection dmCon = new DriverManagerConnection();
		    // conn = dmCon.getConnection(p);
		    conn = dmCon.getConnection(null);
		}
		else
		{
		    logMsg("Using DataSource");
		    DataSourceConnection dsCon = new DataSourceConnection();
		    // conn = dsCon.getConnection(p);
		    conn = dsCon.getConnection(null);
		}
		dbSch = new dbSchema();
		rsSch = new rsSchema();
		stmt = conn.createStatement(/*
					     * ResultSet.TYPE_SCROLL_INSENSITIVE,
					     * ResultSet.CONCUR_READ_ONLY
					     */);
		dbmd = conn.getMetaData();
	    }
	    catch (SQLException ex)
	    {
		logErr("SQL Exception : " + ex.getMessage());
	    }
	}
	catch (Exception e)
	{
	    logErr("Setup Failed!");
	    TestUtil.printStackTrace(e);
	}
    }

    /*
     * @testName: testGetByte22
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * 
     * The getSQLXML(int columnIndex) gets the value of the designated column in
     * the current row of this ResultSet object as a byte in the Java
     * programming language,if the value is SQL NULL, the value returned is null
     */
    @Test
    public void testGetSQLXML() throws Exception
    {
	String retValue;
	String expectValue;
	ResultSet rs = null;
	try
	{
	    String query = queryAll;

	    rs = stmt.executeQuery(query);
	    rs.next();
	    logMsg("Calling getSQLXML on DB XSQS");
	    retValue = rs.getString(1);
	    expectValue = firstLine;
	    retValue = retValue.replaceAll("\n", "");
	    retValue = retValue.replaceAll("  ", "");

	    if (expectValue.trim().equals(retValue))
	    {
		logMsg("getSQLXML method returns : " + retValue);
	    }
	    else
	    {
		logErr("getSQLXML method does not return the expected value from DB XSQS ");
		throw new Exception("Call to getSQLXML is Failed!");
	    }
	}

	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    throw new Exception("Call to getSQLXML is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to getSQLXML is Failed!", e);
	}
	finally
	{
	    try
	    {
		rs.close();
	    }
	    catch (Exception e)
	    {
	    }
	    ;
	}
    }

    /* cleanup */
    @AfterClass
    public static void cleanup() throws Exception
    {
	try
	{
	    // Close the Statement
	    stmt.close();
	    // Close the database
	    rsSch.dbUnConnect(conn);
	    logMsg("Cleanup ok;");
	}
	catch (Exception e)
	{
	    logErr("An error occurred while closing the database connection");
	}
    }
}
